package com.egao.common.module.marketPush.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.egao.common.core.annotation.OperLog;
import com.egao.common.core.web.BaseController;
import com.egao.common.core.web.JsonResult;
import com.egao.common.core.web.PageParam;
import com.egao.common.core.web.PageResult;
import com.egao.common.module.marketPush.entity.MarketPush;
import com.egao.common.module.marketPush.entity.MarketPushRecord;
import com.egao.common.module.marketPush.param.MarketPushParams;
import com.egao.common.module.marketPush.service.MarketPushRecordService;
import com.egao.common.module.marketPush.service.MarketPushService;
import com.egao.common.module.student.entity.Student;
import com.egao.common.module.student.service.StudentService;
import com.egao.common.module.system.constants.DictConstants;
import com.egao.common.module.system.entity.Major;
import com.egao.common.module.system.entity.Year;
import com.egao.common.module.system.service.CollegeService;
import com.egao.common.module.system.service.DictService;
import com.egao.common.module.system.service.MajorService;
import com.egao.common.module.system.service.YearService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author lym
 * @date 2024/5/27 15:00
 */
@Api(tags = "就业市场推送消息")
@RestController
@RequestMapping("${api}")
public class MarketPushController extends BaseController {

    @Autowired
    private YearService yearService;
    @Autowired
    private CollegeService collegeService;
    @Autowired
    private MajorService majorService;
    @Autowired
    private DictService dictService;
    @Autowired
    private MarketPushService marketPushService;
    @Autowired
    private MarketPushRecordService marketPushRecordService;
    @Autowired
    private StudentService studentService;

    @PreAuthorize("hasAuthority('market-push')")
    @ApiOperation("查询记录")
    @GetMapping("/market-push-page")
    public PageResult<MarketPush> page(HttpServletRequest request) {
        PageParam<MarketPush> pageParam = new PageParam<>(request);
        return new PageResult<>(marketPushService.listPage(pageParam), pageParam.getTotal());
    }

    @PreAuthorize("hasAuthority('market-push')")
    @ApiOperation("查询详情记录")
    @GetMapping("/market-push-record-page")
    public PageResult<MarketPushRecord> recordPage(HttpServletRequest request) {
        PageParam<MarketPushRecord> pageParam = new PageParam<>(request);
        return new PageResult<>(marketPushRecordService.listPage(pageParam), pageParam.getTotal());
    }

    @PreAuthorize("hasAuthority('market-push')")
    @ApiOperation("查询条件")
    @GetMapping("/market-push/conditions")
    public JsonResult conditions() {
        Map<String, Object> map = new HashMap<>();
        map.put("year", yearService.listAll().getData());
        map.put("college", collegeService.listAll(null, ""));
        map.put("major", majorService.list(new LambdaQueryWrapper<Major>().groupBy(Major::getCode)));

        List<Student> studentList = studentService.list(new LambdaQueryWrapper<Student>().select(Student::getBjhm).groupBy(Student::getBjhm));
        List<String> bjhm = studentList.stream().filter(d -> d != null && !StrUtil.isBlank(d.getBjhm())).map(Student::getBjhm).collect(Collectors.toList());
        map.put("bjhm", bjhm);

        map.put("xl", dictService.cacheList(DictConstants.TYPE_XL));
        map.put("xw", dictService.cacheList(DictConstants.TYPE_XW));
        map.put("xwlb", dictService.cacheList(DictConstants.TYPE_XWLB));
        map.put("pyfs", dictService.cacheList(DictConstants.TYPE_PYFS));
        map.put("cxsy", dictService.cacheList(DictConstants.TYPE_CXSYDM));
        map.put("xxfs", dictService.cacheList(DictConstants.TYPE_XXFS));
        map.put("syszd", dictService.cacheList(DictConstants.TYPE_SYSZD));
        map.put("zxjh", dictService.cacheList(DictConstants.TYPE_ZXJH));
        map.put("xjzt", dictService.cacheList(DictConstants.TYPE_XJZT));
        map.put("jzbfdxbz", dictService.cacheList(DictConstants.TYPE_JZBFDXBZ));
        map.put("knslb", dictService.cacheList(DictConstants.TYPE_KNSLB));
        map.put("zzmm", dictService.cacheList(DictConstants.TYPE_ZZMM));
        map.put("mz", dictService.cacheList(DictConstants.TYPE_MZ));
        map.put("hyzk", dictService.cacheList(DictConstants.TYPE_HYZK));
        return JsonResult.ok().setData(map);
    }

    @PreAuthorize("hasAuthority('market-push')")
    @ApiOperation("查询人数")
    @PostMapping("/market-push/number")
    public JsonResult number(@RequestBody MarketPushParams marketPushParams) {
        List<String> list = marketPushService.listXh(marketPushParams);
        return JsonResult.ok().setData(list.size());
    }

    @PreAuthorize("hasAuthority('market-push')")
    @OperLog(value = "就业市场推送消息", desc = "企业号推送", result = true)
    @ApiOperation("发送通知")
    @PostMapping("/market-push/send")
    public JsonResult send(@RequestBody MarketPushParams marketPushParams) {
        return marketPushService.send(marketPushParams, getLoginUserId());
    }

    @PreAuthorize("hasAuthority('market-push')")
    @ApiOperation(value = "导出")
    @GetMapping("/market-push/export")
    public void export(Integer id, HttpServletResponse response) {
        marketPushService.exportExcel(id, response);
    }
}
